import timePlugIn from "../../ipha_tool/TimePlugIn";
import gameConfig from "../config/gameConfig";
import Frame from "../frameNamespace";
import { sers } from "../system/OverallSystem ";

export default class UserService extends Frame.ServiceBase {
    protected _serviceName: string = "用户服务";

    private readonly powerKey: string = "user-power";
    private readonly coinKey: string = "coin-power";

    private readonly powerRestoresScheduleKey: string = "power_restores_schedule";

    static readonly infinityPowerDate: string = "inifinity-power-date";
    static readonly infinityPowerModel: string = "inifinity-power-model";

    static readonly changwanModel: string = "changwan-model";
    static readonly changwanModelDate: string = "changwan-model-date";



    static readonly nowLevel:string = "nowLevel"
    static readonly gameId:string ="gameId"
    static readonly getGameList:string = "getGameList"
    static readonly GameLevelData:string = "GameLevelData"

    static readonly GuanqiaID:string = "GuanqiaID"
    static readonly GuanqiaData:string = "GuanqiaData"
    static readonly passNumber:string = "passNumber"

    static readonly passlevelDate:string = "passlevelDate"

    static readonly Pukepai:string = "PukepaiData"
    static readonly UserData:string = "UserData"
    static readonly CheckScene:string = "CheckScene"

    static readonly Majiang2:string = "Majiang2"
    static readonly GuanqiaGameList:string = "GuanqiaGameList"
    static readonly Zhuti:string ="Zhuti"
    static readonly ZhutiBg:string ="ZhutiBg"
    static readonly ZhutiData:string = "ZhutiData"
    static readonly SignInList:string = "SignInList"
    static readonly TiyanGame:string = "TiyanGame"
    static readonly PaiduiData:string = "PaiduiData"
    static readonly LiushuiData:string = "LiushuiData"
    static readonly NowScene:string = "NowScene"

    static readonly SettingData:string = "SettingData"
    static readonly PeiduiData:string = "PeiduiData"
    static readonly HuahuaData:string = "HuahuaData"
    static readonly DaxiguaData:string = "DaxiguaData"
    static readonly FanpaiData:string = "FanpaiData"
    static readonly DaxiguaMapData:string = "DaxiguaMapData"
    static readonly DaxiguaData2:string = "DaxiguaData2"
    static readonly DaxiguaMapData2:string = "DaxiguaMapData2"
    static readonly TiantiData:string = "TiantiData"
    static readonly DaxiguaData3:string = "DaxiguaData3"
    static readonly DaxiguaMapData3:string = "DaxiguaMapData3"
    static readonly DaxiguaData4:string = "DaxiguaData4"
    static readonly DaxiguaMapData4:string = "DaxiguaMapData4"
    static readonly Fandabing:string = "Fandabing"
    static readonly FandabingMap:string = "FandabingMap"
    static readonly XianyuData:string = "XianyuData"
    static readonly FanKuaiData:string = "FanKuaiData"


    constructor() {
        super();

        this._Initialize();
    }

    _Initialize(): void {
        super._Initialize();
    }

    private m_Power: number = 5;
    get power() { return this.m_Power; }

    private m_Coin: number = 0;
    get coin() { return this.m_Coin; }

    _initPower() {
        const date = Date.now();
        let preDate = sers().localStorageSrv.get(UserService.infinityPowerDate);
        let isInfinityPowerModel = Number(sers().localStorageSrv.get(UserService.infinityPowerModel)) != 0;
        let isDateOut24 = date - Number(preDate) >= 86400 * 1e3;


        let ModeDate = sers().localStorageSrv.get(UserService.changwanModelDate);
        let Mode = Number(sers().localStorageSrv.get(UserService.changwanModel)) != 0;
        let DateOut24 = date - Number(ModeDate) >= 86400 * 1e3;

        if(ModeDate != null && ModeDate != "" && Mode && DateOut24){
            console.log(ModeDate)
            console.log(DateOut24)
            // sers().userSrv.setpassNumber(0)
            // 无限畅玩模式结束
            sers().localStorageSrv.set(UserService.changwanModel, 0);
            sers().localStorageSrv.set(UserService.changwanModelDate, date);
        }

        //XXX
        if (preDate != null && preDate != "" && isInfinityPowerModel && isDateOut24) {
            //如果无限体力模式大于24小时
            this.setPower(gameConfig.MAX_POWER);
            UserService.unlockCnt = 0
            sers().localStorageSrv.set(UserService.infinityPowerModel, 0);
            sers().localStorageSrv.set(UserService.infinityPowerDate, date);
        } else {
            let power = sers().localStorageSrv.get(this.powerKey);
           
            // if (power != null && power != "") {
            //     this.m_Power = JSON.parse(power);
            // } else {
            //     this.m_Power = gameConfig.MAX_POWER;
            // }
            console.log(power)
            console.log(+power)
            console.log(power != null)
            console.log(power != "")
            console.log(+power>=0)

            if(power === ""){
                console.log('默认体力')
                this.m_Power = gameConfig.MAX_POWER;
            }else{
                if(power != null && +power>=0 ){
                    // this.m_Power = JSON.parse(power);
                    this.setPower(+power);
                }else{
                    console.log('恢复最大体力')
                    this.m_Power = gameConfig.MAX_POWER;
                }
            }

           

            if (this.isPowerLess()) {
                //离线回复体力
                const preDate = timePlugIn().getPreviousPlayDate2();
                const nowDate = Date.now();

              

                let offset = Math.floor((nowDate - preDate) / 1000);
                let resume = Math.trunc(offset / gameConfig.POWER_RESUME_TIME);
               
                this.m_Power += resume;
                this.m_Power = Math.min(gameConfig.MAX_POWER, this.m_Power);
                console.log("离线回复体力 ",resume," 点");
            }
        }

        sers().localStorageSrv.set(this.powerKey, this.m_Power);
        //设置自动回复体力
        timePlugIn().setScheduleOneSeconds(this.powerRestoresScheduleKey, this.restoresPower.bind(this));
    }

    _initCoin() {
        let coin = sers().localStorageSrv.get(this.coinKey);
        if (coin != null && coin != "") {
            this.m_Coin = JSON.parse(coin);
        }

        sers().localStorageSrv.set(this.coinKey, this.m_Coin);
    }

    private restoresPowerTimer: number = 0;
    private restoresPower() {
        if (this.isPowerLess()) {
            this.restoresPowerTimer++;
            if (this.restoresPowerTimer >= gameConfig.POWER_RESUME_TIME) {
                this.addPower();
                this.restoresPowerTimer = 0;
            }
        } else {
            this.restoresPowerTimer = 0;
        }
    }

    setPower(power: number) {
        this.m_Power = power;
        sers().localStorageSrv.set(this.powerKey, this.m_Power);
    }

    addPower(add: number = 1) {
        this.m_Power += add;
        if(!window['tt'] && !window['wx'] && this.m_Power<=0){
            this.m_Power = gameConfig.MAX_POWER
        }
        sers().localStorageSrv.set(this.powerKey, this.m_Power);
    }

    /**
     * 进入模式扣除体力
     * @param need 进入所需体力值
     * @param conformCB 体力足够,可以进入模式回调
     * @param lessThanCB 体力不足回调
     */
    enterModeDeductPower(need: number, conformCB: () => void, lessThanCB?: () => void) {
        if (this.m_Power >= need) {
            this.addPower(-need);
            conformCB && conformCB();
        } else {
            lessThanCB && lessThanCB();
        }
    }

    isPowerLess() {
        return this.m_Power < gameConfig.MAX_POWER;
    }

    getRePowerTimer() {
        return gameConfig.POWER_RESUME_TIME - this.restoresPowerTimer;
    }



    setCoin(coin: number) {
        this.m_Coin = coin;
        sers().localStorageSrv.set(this.coinKey, this.m_Coin);
    }

    addCoin(add: number) {
        this.m_Coin += add;
        sers().localStorageSrv.set(this.coinKey, this.m_Coin);
    }

    
    static maxLevel = 10
    static gameLevel =[
        10,
        1
    ]
    /**
     * 
     * @param n 设置当前关卡
     */
    setLevel(n:number){
        if(n>UserService.maxLevel){
            sers().localStorageSrv.set(UserService.nowLevel,UserService.maxLevel)
        }else {
            sers().localStorageSrv.set(UserService.nowLevel,n)
        }
        cc.log('游戏关卡'+ this.getLevel())
    }

    getLevel(): Number{
        let l = +sers().localStorageSrv.get(UserService.nowLevel) || 1;
        return l
    }

    setGameId(n:Number){
       
        sers().localStorageSrv.set(UserService.gameId,n)
        cc.log('当前游戏ID'+ this.getGameId())
    }

    getGameId(): Number{
        let l = +sers().localStorageSrv.get(UserService.gameId) || 0;
        return l
    }

    setGameList(n:Number){
        sers().localStorageSrv.set(UserService.getGameList,n)
        cc.log('当前游戏List'+ this.getGameList())
    }

    getGameList(): Number{
        let l = +sers().localStorageSrv.get(UserService.getGameList) || 0;
        return l
    }

    getGameLevelData(){
        let data =JSON.parse(sers().localStorageSrv.get(UserService.GameLevelData) || null)
        if(!!!data){
            data = {
                lock:[],
                pass:[],
            }
        }
        sers().localStorageSrv.set(UserService.GameLevelData,JSON.stringify(data))
        return data
    }
    
    static zs_level_switch =null

    static guanqia = null

    static unlockCnt = 0

    setGameLevelData(data){
        sers().localStorageSrv.set(UserService.GameLevelData,JSON.stringify(data))
    }

    setGuanqiaID(n:Number){
       
        sers().localStorageSrv.set(UserService.GuanqiaID,n)
        
        cc.log('当前关卡ID'+ this.getGuanqiaID())
    }

    getGuanqiaID(): Number{
        let l = +sers().localStorageSrv.get(UserService.GuanqiaID) || 0;
        return l
    }

    setGuanqiaData(data){
        sers().localStorageSrv.set(UserService.GuanqiaData,JSON.stringify(data))
    }

    getGuanqiaData(){
        let data =JSON.parse(sers().localStorageSrv.get(UserService.GuanqiaData) || null)
        return data
    }

    static IPAD = false
    static daren = false

    static level = 1

    public static vivoSDKdata = null

    static levelNumber = 0

    getpassNumber(){
        let data = +sers().localStorageSrv.get(UserService.passNumber) || 0;
        
        return data
    }

    setpassNumber(n:Number){       
        sers().localStorageSrv.set(UserService.passNumber,n)
        cc.log('过关次数'+ this.getpassNumber())
    }

    setStoreData(data){
        sers().localStorageSrv.set(UserService.Pukepai,JSON.stringify(data))
    }

    getStoreData(){
        let data =JSON.parse(sers().localStorageSrv.get(UserService.Pukepai) || null)
        return data
    }

    setUserData(data){
        sers().localStorageSrv.set(UserService.UserData,JSON.stringify(data))
    }

    getUserData(){
        let data =JSON.parse(sers().localStorageSrv.get(UserService.UserData) || null)
        if(!!!data){
            data = {
                city:'',
                level:1,
                maxLevel:1
            }
        }
        return data
    }

    getCheckScene(){
        let data =sers().localStorageSrv.get(UserService.CheckScene|| null)
        return data
    }

    setCheckScene(data){
        sers().localStorageSrv.set(UserService.CheckScene,data)
    }


    setStoreMajiang2Data(data){
        sers().localStorageSrv.set(UserService.Majiang2,JSON.stringify(data))
    }

    getStoreMajiang2Data(){
        let data =JSON.parse(sers().localStorageSrv.get(UserService.Majiang2) || null)
        if(!!!data){
            data = {
                over :false
            }
        }
        return data
    }

    getGuanqiaGameList() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.GuanqiaGameList) || null)
        if(!!!data){
            data = []
        }
        return data
    }

    setGuanqiaGameList(data) {
        sers().localStorageSrv.set(UserService.GuanqiaGameList, JSON.stringify(data))
    }

    getZhuti(){
        let data = sers().localStorageSrv.get(UserService.Zhuti) || 0
        return data
    }

    setZhuti(data){
        sers().localStorageSrv.set(UserService.Zhuti,data)
    }

    getZhutiBg(){
        let data = sers().localStorageSrv.get(UserService.ZhutiBg) || 0
        return data
    }

    setZhutiBg(data){
        sers().localStorageSrv.set(UserService.ZhutiBg,data)
    }

    getZhutiData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.ZhutiData) || null)
        if(!!!data){
            data = []
        }
        return data
    }

    setZhutiData(data) {
        sers().localStorageSrv.set(UserService.ZhutiData, JSON.stringify(data))
    }


    getSignInList() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.SignInList) || null)
        if(!!!data){
            data = {
                one:false,
                list:[],
                lastTime:null
            }
        }
        return data
    }

    setSignInList(data) {
        sers().localStorageSrv.set(UserService.SignInList, JSON.stringify(data))
    }

    getTiyanGame() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.TiyanGame) || null)
        if(!!!data){
            data = []
        }
        return data
    }

    setTiyanGame(data) {
        sers().localStorageSrv.set(UserService.TiyanGame, JSON.stringify(data))
    }

    setPaiduiData(data) {
        sers().localStorageSrv.set(UserService.PaiduiData, JSON.stringify(data))
    }

    getPaiduiData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.PaiduiData) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }

    setXianyuData(data) {
        sers().localStorageSrv.set(UserService.XianyuData, JSON.stringify(data))
    }

    getXianyuData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.XianyuData) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }


    setLiushuiData(data) {
        sers().localStorageSrv.set(UserService.LiushuiData, JSON.stringify(data))
    }

    getLiushuiData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.LiushuiData) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }


    getNowScene(){
        let data = sers().localStorageSrv.get(UserService.NowScene) || 0
        return data
    }

    setNowScene(data){
        sers().localStorageSrv.set(UserService.NowScene,data)
    }

    getSettingData(){
        let data = JSON.parse(sers().localStorageSrv.get(UserService.SettingData) || null)
        if(!!!data){
            data= [true,true,true,true,true,0]
        }
        return data
    }

    setSettingData(data){
        sers().localStorageSrv.set(UserService.SettingData, JSON.stringify(data))
    }


    setPeiduiData(data) {
        sers().localStorageSrv.set(UserService.PeiduiData, JSON.stringify(data))
    }

    getPeiduiData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.PeiduiData) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }


    setHuahuaData(data) {
        sers().localStorageSrv.set(UserService.HuahuaData, JSON.stringify(data))
    }

    getHuahuaData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.HuahuaData) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }

    setDaxiguaData(data) {
        sers().localStorageSrv.set(UserService.DaxiguaData, JSON.stringify(data))
    }

    getDaxiguaData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.DaxiguaData) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }

    setDaxiguaMapData(data) {
        sers().localStorageSrv.set(UserService.DaxiguaMapData, JSON.stringify(data))
    }

    getDaxiguaMapData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.DaxiguaMapData) || null)
        if(!!!data){
            data= {
                
            }
        }
        return data
    }


    setDaxiguaData2(data) {
        sers().localStorageSrv.set(UserService.DaxiguaData2, JSON.stringify(data))
    }

    getDaxiguaData2() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.DaxiguaData2) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }


    setDaxiguaMapData2(data) {
        sers().localStorageSrv.set(UserService.DaxiguaMapData2, JSON.stringify(data))
    }

    getDaxiguaMapData2() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.DaxiguaMapData2) || null)
        if(!!!data){
            data= {
                
            }
        }
        return data
    }


    setDaxiguaData3(data) {
        sers().localStorageSrv.set(UserService.DaxiguaData3, JSON.stringify(data))
    }

    getDaxiguaData3() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.DaxiguaData3) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }


    setDaxiguaMapData3(data) {
        sers().localStorageSrv.set(UserService.DaxiguaMapData3, JSON.stringify(data))
    }

    getDaxiguaMapData3() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.DaxiguaMapData3) || null)
        if(!!!data){
            data= {
                
            }
        }
        return data
    }



    setDaxiguaData4(data) {
        sers().localStorageSrv.set(UserService.DaxiguaData4, JSON.stringify(data))
    }

    getDaxiguaData4() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.DaxiguaData4) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }


    setDaxiguaMapData4(data) {
        sers().localStorageSrv.set(UserService.DaxiguaMapData4, JSON.stringify(data))
    }

    getDaxiguaMapData4() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.DaxiguaMapData4) || null)
        if(!!!data){
            data= {
                
            }
        }
        return data
    }

    setFanpaiData(data) {
        sers().localStorageSrv.set(UserService.FanpaiData, JSON.stringify(data))
    }

    getFanpaiData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.FanpaiData) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }


    setFanKuaiData(data) {
        sers().localStorageSrv.set(UserService.FanKuaiData, JSON.stringify(data))
    }

    getFanKuaiData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.FanKuaiData) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }
    

    setTiantiData(data) {
        sers().localStorageSrv.set(UserService.TiantiData, JSON.stringify(data))
    }

    getTiantiData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.TiantiData) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }


    setFandabingData(data) {
        sers().localStorageSrv.set(UserService.Fandabing, JSON.stringify(data))
    }

    getFandabingData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.Fandabing) || null)
        if(!!!data){
            data= {
                level:1
            }
        }
        return data
    }


    setFandabingMapData(data) {
        sers().localStorageSrv.set(UserService.FandabingMap, JSON.stringify(data))
    }

    getFandabingMapData() {
        let data = JSON.parse(sers().localStorageSrv.get(UserService.FandabingMap) || null)
        if(!!!data){
            data= {
                
            }
        }
        return data
    }


}